package cn.cmcc.online.smsapi.ui.widget;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.util.SimpleAnimationListener;

/**
 * Created by ZhangYifan on 2017/6/14.
 */

public class ExpandButton extends LinearLayout implements View.OnClickListener {
    public static final int STATE_MORE = 0;
    public static final int STATE_RETRACT = 1;

    private TextView mTv;
    private Triangle mTriangle;
    private int state;

    private ValueAnimator animatorDisappear;
    private ValueAnimator animatorAppear;

    private boolean withAnimation;
    private ExpandStateChangedListener stateChangedListener;

    public ExpandButton(Context context) {
        super(context);

        setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        setLayoutParams(new LayoutParams(DensityUtil.dp2px(context, 70), DensityUtil.dp2px(context, 28)));
        state = STATE_RETRACT;
        setOnClickListener(this);

        mTv = new TextView(context);
        mTv.setText("更多");
        addView(mTv);

        mTriangle = new Triangle(context);
        LayoutParams triangleParams = new LayoutParams(DensityUtil.dp2px(context, 7), DensityUtil.dp2px(context, 7));
        triangleParams.setMargins(DensityUtil.dp2px(context, 4), 0, DensityUtil.dp2px(context, 8), 0);
        addView(mTriangle, triangleParams);
    }

    public void setColor(int color) {
        mTv.setTextColor(color);
        mTriangle.setColor(color);
    }

    public void setStateChangedListener(ExpandStateChangedListener stateChangedListener) {
        this.stateChangedListener = stateChangedListener;
    }

    @Override
    public void onClick(View v) {
        if (state == STATE_RETRACT) {
            if (stateChangedListener != null) {
                stateChangedListener.showMore();
            }
            setStateMore();
        } else {
            if (stateChangedListener != null) {
                stateChangedListener.showLess();
            }
            setStateRetract();
        }
    }

    public void setStateRetract() {
        if (!withAnimation) {
            mTv.setText("更多");
            mTriangle.setOritionDown();
            state = STATE_RETRACT;

        } else {
            animatorDisappear.removeAllListeners();
            animatorDisappear.addListener(new SimpleAnimationListener(){
                @Override
                public void onAnimationEnd(Animator animation) {
                    mTv.setText("更多");
                    mTriangle.setOritionDown();
                    state = STATE_RETRACT;
                }
            });
            AnimatorSet animatorSet=new AnimatorSet();
            animatorSet.play(animatorDisappear).before(animatorAppear);
            animatorSet.start();
        }
    }

    public void setStateMore() {
        if (!withAnimation) {
            mTv.setText("收起");
            mTriangle.setOritionUp();
            state = STATE_MORE;
        } else {
            animatorDisappear.removeAllListeners();
            animatorDisappear.addListener(new SimpleAnimationListener(){
                @Override
                public void onAnimationEnd(Animator animation) {
                    mTv.setText("收起");
                    mTriangle.setOritionUp();
                    state = STATE_MORE;
                }
            });
            AnimatorSet animatorSet=new AnimatorSet();
            animatorSet.play(animatorDisappear).before(animatorAppear);
            animatorSet.start();
        }
    }

    public void setWithAnimation(boolean withAnimation) {
        this.withAnimation = withAnimation;
        if (withAnimation) {
            animatorDisappear = new ValueAnimator().ofFloat(1F, 0F).setDuration(200);
            animatorAppear = new ValueAnimator().ofFloat(0F, 1F).setDuration(200);
            animatorDisappear.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    setAlpha((Float) animation.getAnimatedValue());
                }
            });
            animatorAppear.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    setAlpha((Float) animation.getAnimatedValue());
                }
            });
        }

    }

    public interface ExpandStateChangedListener {
        void showMore();

        void showLess();
    }

    public static class Triangle extends View {
        private Context mContext;
        private boolean isUp;
        private Paint paint;

        public Triangle(Context context) {
            super(context);
            mContext = context;
            paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLACK);
        }

        public void setOritionUp() {
            isUp = true;
            invalidate();
        }

        public void setOritionDown() {
            isUp = false;
            invalidate();
        }

        public void setColor(int color) {
            paint.setColor(color);
            invalidate();
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            float dp7 = 1F * DensityUtil.dp2px(mContext, 7);
            float dp6 = 1F * DensityUtil.dp2px(mContext, 6);
            float dp1 = 1F * DensityUtil.dp2px(mContext, 1);

            Path path = new Path();
            if (isUp) {
                path.moveTo(0, dp6);
                path.lineTo(dp7, dp6);
                path.lineTo(dp7 / 2, 0);
                path.close();
            } else {
                path.moveTo(0, dp1);
                path.lineTo(dp7, dp1);
                path.lineTo(dp7 / 2, dp7);
                path.close();
            }
            canvas.drawPath(path, paint);

        }
    }
}
